{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 《R 語言忍者秘笈》筆記\n",
    "\n",
    "written by 谢益辉 肖楠 坑主三 坑主四\n",
    "\n",
    "R语言 (R Core Team 2017) 是由统计学家发明的一门程序语言，这个特殊的背景让这门语言在计算机专业人士眼中看起来也许很奇怪：语法松散、数据结构不严谨、充斥着黑魔法，等等。如果能结合数据分析的背景去看待它，就会发现它还是有很多精妙之处的。\n",
    "\n",
    "学一门语言不可能通过两天时间把语法看完了事就行，必须得实战练习：一来巩固语法，二来增加经验值。本书根据<a href=\"http://cos.name/cn/\"> 统计之都论坛 </a> 六年中近六千帖子和三万回帖整理并加入作者的个人经验而写成。我们找的有这样几种帖子：\n",
    "\n",
    "版主们被问得怒火丛生，因为同样的问题被问了八百遍了，但论坛规定版主得修炼内力，所以老实版主回帖提供老链接，其它版主默默；\n",
    "大家讨论得热火朝天，提出各种解决方案，但某网友一行代码扔出来，让所有人都默默；\n",
    "一个帖子当时只有寥寥几个回复，好像没什么人搭理，默默过了两年，楼主突然冒出来说，哇，这一招真管用；\n",
    "一个帖子楼主默默写了很久很久，但根本没有人回复，可能当时大家的查克拉都没到那个量级；\n",
    "总之，我们追求的是高效率的R应用，就像一名忍者，了解大局形势，也善于利用细节，飞檐走壁上天入地，准确而迅速地完成任务。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安裝與配置\n",
    "\n",
    "### for Windows users\n",
    "https://bookdown.org/yihui/r-ninja/setup.html\n",
    "\n",
    "### for macOS users\n",
    "類似於 Windows 的安裝方法，直接在 https://www.r-project.org 找到安裝包下載即可。\n",
    "<br>\n",
    "\n",
    "PS：**可以使用 Jupyter notebook**，只要<a href=\"https://irkernel.github.io/installation/#linux-panel\">按照這個方法</a>去安裝即可。(This note powered by Jupyter notebook)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本運算\n",
    "\n",
    "R 語言跟別的語言一樣，有兩種模式，一個是 **Console**，跟它的名字一樣，在界面上（command line interface）敲入代碼，立刻執行。（也稱為交互式解釋器）\n",
    "[![Screen Shot 2018-07-26 at 21.23.34.png](https://i.loli.net/2018/07/26/5b59cb69e6b8f.png)](https://i.loli.net/2018/07/26/5b59cb69e6b8f.png)\n",
    "\n",
    "``` R \n",
    "> 6 + 9 # This line called “prompt”\n",
    "[1] 15 # This line called \"assignment\"\n",
    "> \n",
    "> x <- 15 \n",
    "> x - 1\n",
    "[1] 14\n",
    "``` \n",
    "\n",
    "上面的例子中 `x <- 15` 的意思就是給 x 這個變量賦值；左邊的變量被分配到右邊的值（很機翻的感覺），且，左邊只能是單一一個變量，不能是下面這種情況：<br>\n",
    "``` R\n",
    "> x + 4 <- 15\n",
    "\n",
    "# the console will jump out the error code: \n",
    "# Error in x + 4 <- 15 : could not find function \"+<-\"\n",
    "```\n",
    "當然 ` -> ` 也是可以用來 assign 一個變量的，如下：\n",
    "<br>\n",
    "``` R \n",
    "> x = 5 \n",
    "> 5*x -> x\n",
    "> x\n",
    "\n",
    "[1] 25\n",
    "``` \n",
    "在 R 語言中，`=` 和 `<-` 都是表達賦值的意思，但更多人會推薦使用 `<-` 是因為它比較~~牛批~~（因為別的語言不會用這個運算符）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "15"
      ],
      "text/latex": [
       "15"
      ],
      "text/markdown": [
       "15"
      ],
      "text/plain": [
       "[1] 15"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "14"
      ],
      "text/latex": [
       "14"
      ],
      "text/markdown": [
       "14"
      ],
      "text/plain": [
       "[1] 14"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "25"
      ],
      "text/latex": [
       "25"
      ],
      "text/markdown": [
       "25"
      ],
      "text/plain": [
       "[1] 25"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# line one\n",
    "6 + 9 \n",
    "\n",
    "# line two \n",
    "x <- 15 \n",
    " \n",
    "x - 1\n",
    "\n",
    "#line three\n",
    "x = 5 \n",
    "5 * x -> x \n",
    "x "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 向量\n",
    "\n",
    "在 R 語言中，很多時候我們會將用它的一個特性：矢量化（vectorized），這也就意味著可以在矢量上進行許多操作；函數 `c()` 用來創建向量：\n",
    "\n",
    "``` R \n",
    "> x <- c(1, -1, 3.5, 2)\n",
    "> x\n",
    "\n",
    "[1]  1.0 -1.0  3.5  2.0\n",
    "```\n",
    "然後，如果我們給這個向量加 2，就相當於給向量的每一項都加了 2；乘方，同理 ：\n",
    "``` R \n",
    "> x + 2\n",
    "> x\n",
    "\n",
    "[1]  3.0 1.0  5.5  4.0\n",
    "\n",
    "> x^2\n",
    "\n",
    "[1] 1.00 1.00 12.25 4.00\n",
    "```\n",
    "這個操作在統計裡很有用：\n",
    "``` R\n",
    "> sum((x - mean(x))^2)\n",
    "\n",
    "[1] 10.69\n",
    "```\n",
    "\n",
    "#### Exercise 2.1.  \n",
    "\n",
    "The weights of five people before and after a diet programme are given in the table. \n",
    "<br>\n",
    "Before | After\n",
    "---- | ---\n",
    "78 | 67\n",
    "72 |  65\n",
    "78 | 79\n",
    "79 | 70\n",
    "105 | 93\n",
    "\n",
    "Read the ‘before’ and ‘after’ values into two different vectors called before and after. Use R to evaluate the amount of weight lost for each participant. What is the average amount of weight lost?\n",
    "<br>\n",
    "> Answer: \n",
    "\n",
    "```R\n",
    "> before <- c(78, 72, 78, 79, 105)\n",
    "\n",
    "> after <- c(67, 65, 79, 70, 93)\n",
    "\n",
    "> lost <- before - after \n",
    "> lost \n",
    "\n",
    "[1] 11  7 -1  9 12\n",
    "> lost / 5\n",
    "\n",
    "[1]  2.2  1.4 -0.2  1.8  2.4\n",
    "```\n",
    "當然，為了方便自己，也為了方便使用者（R 的開發者就是統計學家），R 語言中預設了許多快捷方法來創建一些常見的向量。**使用冒號（colon），可以用來生成整數序列**:\n",
    "\n",
    "```R \n",
    "> 1:10\n",
    "\n",
    "[1]  1  2  3  4  5  6  7  8  9 10\n",
    "\n",
    "> -3:4\n",
    "\n",
    "[1] -3 -2 -1  0  1  2  3  4\n",
    "\n",
    "> 9:5\n",
    "\n",
    "[1] 9 8 7 6 5\n",
    "```\n",
    "\n",
    "如果只有整數序列，這個世界會缺少很多樂趣，這個時候我們就需要 `seq()` 這個函數了，我們來看看他怎麼用：\n",
    "``` R \n",
    "> seq(from=2, to=6, by=0.4)\n",
    "\n",
    " [1] 2.0 2.4 2.8 3.2 3.6 4.0 4.4 4.8 5.2 5.6 6.0\n",
    "\n",
    "> seq(from=-1, to=1, length=6)\n",
    "\n",
    "[1] -1.0 -0.6 -0.2  0.2  0.6  1.0\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "?seq()\n",
    "\n",
    "# This command can check the R Documentation \n",
    "\n",
    "#seq(...)\n",
    "\n",
    "## Default S3 method:\n",
    "#seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),\n",
    " #   length.out = NULL, along.with = NULL, ...)\n",
    "\n",
    "#seq.int(from, to, by, length.out, along.with, ...)\n",
    "\n",
    "#seq_along(along.with)\n",
    "#seq_len(length.out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>2</li>\n",
       "\t<li>2.4</li>\n",
       "\t<li>2.8</li>\n",
       "\t<li>3.2</li>\n",
       "\t<li>3.6</li>\n",
       "\t<li>4</li>\n",
       "\t<li>4.4</li>\n",
       "\t<li>4.8</li>\n",
       "\t<li>5.2</li>\n",
       "\t<li>5.6</li>\n",
       "\t<li>6</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 2\n",
       "\\item 2.4\n",
       "\\item 2.8\n",
       "\\item 3.2\n",
       "\\item 3.6\n",
       "\\item 4\n",
       "\\item 4.4\n",
       "\\item 4.8\n",
       "\\item 5.2\n",
       "\\item 5.6\n",
       "\\item 6\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 2\n",
       "2. 2.4\n",
       "3. 2.8\n",
       "4. 3.2\n",
       "5. 3.6\n",
       "6. 4\n",
       "7. 4.4\n",
       "8. 4.8\n",
       "9. 5.2\n",
       "10. 5.6\n",
       "11. 6\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] 2.0 2.4 2.8 3.2 3.6 4.0 4.4 4.8 5.2 5.6 6.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>-1</li>\n",
       "\t<li>-0.6</li>\n",
       "\t<li>-0.2</li>\n",
       "\t<li>0.2</li>\n",
       "\t<li>0.6</li>\n",
       "\t<li>1</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item -1\n",
       "\\item -0.6\n",
       "\\item -0.2\n",
       "\\item 0.2\n",
       "\\item 0.6\n",
       "\\item 1\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. -1\n",
       "2. -0.6\n",
       "3. -0.2\n",
       "4. 0.2\n",
       "5. 0.6\n",
       "6. 1\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] -1.0 -0.6 -0.2  0.2  0.6  1.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "seq(from = 2, to = 6, by = 0.4) # this ‘by' is the common difference \n",
    "seq(from=-1, to=1, length=6) # this 'length' is the length of the sequence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "這個函數看起來很牛批，但是如果我們想要在一數列全都是 5 的呢：$[5,5,5,5,5 \\dots$，這是就需要一個 `rep()` 了。（同樣，我們可以用 `?rep()` 去查它的用法）\n",
    "\n",
    "```R \n",
    "> rep(5,3)\n",
    "\n",
    "[1] 5 5 5\n",
    "\n",
    "> rep(2:5,each=3)\n",
    "\n",
    "[1] 2 2 2 3 3 3 4 4 4 5 5 5\n",
    "\n",
    "> rep(-1:3, length.out=10)\n",
    "\n",
    "[1] -1  0  1  2  3 -1  0  1  2  3\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>5</li>\n",
       "\t<li>5</li>\n",
       "\t<li>5</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 5\n",
       "\\item 5\n",
       "\\item 5\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 5\n",
       "2. 5\n",
       "3. 5\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] 5 5 5"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>2</li>\n",
       "\t<li>2</li>\n",
       "\t<li>2</li>\n",
       "\t<li>3</li>\n",
       "\t<li>3</li>\n",
       "\t<li>3</li>\n",
       "\t<li>4</li>\n",
       "\t<li>4</li>\n",
       "\t<li>4</li>\n",
       "\t<li>5</li>\n",
       "\t<li>5</li>\n",
       "\t<li>5</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 2\n",
       "\\item 2\n",
       "\\item 2\n",
       "\\item 3\n",
       "\\item 3\n",
       "\\item 3\n",
       "\\item 4\n",
       "\\item 4\n",
       "\\item 4\n",
       "\\item 5\n",
       "\\item 5\n",
       "\\item 5\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 2\n",
       "2. 2\n",
       "3. 2\n",
       "4. 3\n",
       "5. 3\n",
       "6. 3\n",
       "7. 4\n",
       "8. 4\n",
       "9. 4\n",
       "10. 5\n",
       "11. 5\n",
       "12. 5\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] 2 2 2 3 3 3 4 4 4 5 5 5"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>-1</li>\n",
       "\t<li>0</li>\n",
       "\t<li>1</li>\n",
       "\t<li>2</li>\n",
       "\t<li>3</li>\n",
       "\t<li>-1</li>\n",
       "\t<li>0</li>\n",
       "\t<li>1</li>\n",
       "\t<li>2</li>\n",
       "\t<li>3</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item -1\n",
       "\\item 0\n",
       "\\item 1\n",
       "\\item 2\n",
       "\\item 3\n",
       "\\item -1\n",
       "\\item 0\n",
       "\\item 1\n",
       "\\item 2\n",
       "\\item 3\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. -1\n",
       "2. 0\n",
       "3. 1\n",
       "4. 2\n",
       "5. 3\n",
       "6. -1\n",
       "7. 0\n",
       "8. 1\n",
       "9. 2\n",
       "10. 3\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] -1  0  1  2  3 -1  0  1  2  3"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rep(5,3)\n",
    "rep(2:5,each=3)\n",
    "rep(-1:3, length.out=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我們也可以利用 R 語言的矢量化這個特點來創造一些更好玩的序列：\n",
    "```R\n",
    "> 2^(0:10)\n",
    "\n",
    "[1] 1 2 4 8 16 32 64 128 256 512 1024\n",
    "\n",
    "> 1:3 + rep(seq(from=0,by=10,to=30), each=3)\n",
    "\n",
    "[1] 1 2 3 11 12 13 21 22 23 31 32 33\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>1</li>\n",
       "\t<li>2</li>\n",
       "\t<li>4</li>\n",
       "\t<li>8</li>\n",
       "\t<li>16</li>\n",
       "\t<li>32</li>\n",
       "\t<li>64</li>\n",
       "\t<li>128</li>\n",
       "\t<li>256</li>\n",
       "\t<li>512</li>\n",
       "\t<li>1024</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 1\n",
       "\\item 2\n",
       "\\item 4\n",
       "\\item 8\n",
       "\\item 16\n",
       "\\item 32\n",
       "\\item 64\n",
       "\\item 128\n",
       "\\item 256\n",
       "\\item 512\n",
       "\\item 1024\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 1\n",
       "2. 2\n",
       "3. 4\n",
       "4. 8\n",
       "5. 16\n",
       "6. 32\n",
       "7. 64\n",
       "8. 128\n",
       "9. 256\n",
       "10. 512\n",
       "11. 1024\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1]    1    2    4    8   16   32   64  128  256  512 1024"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>1</li>\n",
       "\t<li>2</li>\n",
       "\t<li>3</li>\n",
       "\t<li>11</li>\n",
       "\t<li>12</li>\n",
       "\t<li>13</li>\n",
       "\t<li>21</li>\n",
       "\t<li>22</li>\n",
       "\t<li>23</li>\n",
       "\t<li>31</li>\n",
       "\t<li>32</li>\n",
       "\t<li>33</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 1\n",
       "\\item 2\n",
       "\\item 3\n",
       "\\item 11\n",
       "\\item 12\n",
       "\\item 13\n",
       "\\item 21\n",
       "\\item 22\n",
       "\\item 23\n",
       "\\item 31\n",
       "\\item 32\n",
       "\\item 33\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 1\n",
       "2. 2\n",
       "3. 3\n",
       "4. 11\n",
       "5. 12\n",
       "6. 13\n",
       "7. 21\n",
       "8. 22\n",
       "9. 23\n",
       "10. 31\n",
       "11. 32\n",
       "12. 33\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1]  1  2  3 11 12 13 21 22 23 31 32 33"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    " 2^(0:10) \n",
    "1:3 + rep(seq(from=0,to=30, by=10), each=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最后一个例子展示了回收，这也是矢量化的一个重要部分。 如果我们对两个不同长度的矢量执行二进制运算（例如+），则反复使用较短的运算，直到操作已应用于较长的一个条目。 如果较长的长度不是较短长度的倍数，则给出警告。\n",
    "```R\n",
    "> 1:10 * c(-1,1)\n",
    "\n",
    "[1]-1 2-3 4-5 6-7 8-910\n",
    "\n",
    "> 1:7 * 1:2\n",
    "\n",
    "Warning:  longer object length is not a multiple of shorter object\n",
    "length\n",
    "\n",
    "[1] 1 4 3 8 5 12 7\n",
    "```\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>-1</li>\n",
       "\t<li>2</li>\n",
       "\t<li>-3</li>\n",
       "\t<li>4</li>\n",
       "\t<li>-5</li>\n",
       "\t<li>6</li>\n",
       "\t<li>-7</li>\n",
       "\t<li>8</li>\n",
       "\t<li>-9</li>\n",
       "\t<li>10</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item -1\n",
       "\\item 2\n",
       "\\item -3\n",
       "\\item 4\n",
       "\\item -5\n",
       "\\item 6\n",
       "\\item -7\n",
       "\\item 8\n",
       "\\item -9\n",
       "\\item 10\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. -1\n",
       "2. 2\n",
       "3. -3\n",
       "4. 4\n",
       "5. -5\n",
       "6. 6\n",
       "7. -7\n",
       "8. 8\n",
       "9. -9\n",
       "10. 10\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] -1  2 -3  4 -5  6 -7  8 -9 10"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning message in 1:7 * 1:2:\n",
      "\"longer object length is not a multiple of shorter object length\""
     ]
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>1</li>\n",
       "\t<li>4</li>\n",
       "\t<li>3</li>\n",
       "\t<li>8</li>\n",
       "\t<li>5</li>\n",
       "\t<li>12</li>\n",
       "\t<li>7</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 1\n",
       "\\item 4\n",
       "\\item 3\n",
       "\\item 8\n",
       "\\item 5\n",
       "\\item 12\n",
       "\\item 7\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 1\n",
       "2. 4\n",
       "3. 3\n",
       "4. 8\n",
       "5. 5\n",
       "6. 12\n",
       "7. 7\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1]  1  4  3  8  5 12  7"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "1:10 * c(-1,1)\n",
    "1:7 * 1:2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 2.3. \n",
    "Create the following vectors in R using seq() and rep().<br>\n",
    "(i) 1,1.5,2,2.5,...,12<br>\n",
    "(ii) 1,8,27,64,...,1000.<br>\n",
    "(iii) $1,−\\frac{1}{2}, \\frac{1}{3},−\\frac{1}{4},...,−\\frac{1}{100}$<br>\n",
    "(iv) $1,3,6,10,15,...,\\sum^n_{i=1}i,...,210$ [look up ?cumsum].\n",
    "<br>\n",
    "**Answer**\n",
    "```R\n",
    "#(i)\n",
    "seq(from = 1,to = 12, by = 0.5)\n",
    "#(ii)\n",
    "1:10 * seq(from = 1,to = 10, by = 1)^2\n",
    "#(iii)\n",
    "1/(1:100) * (-1)^(1:100)\n",
    "#(iv)\n",
    "\n",
    "cumsum(1:210)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>1</li>\n",
       "\t<li>1.5</li>\n",
       "\t<li>2</li>\n",
       "\t<li>2.5</li>\n",
       "\t<li>3</li>\n",
       "\t<li>3.5</li>\n",
       "\t<li>4</li>\n",
       "\t<li>4.5</li>\n",
       "\t<li>5</li>\n",
       "\t<li>5.5</li>\n",
       "\t<li>6</li>\n",
       "\t<li>6.5</li>\n",
       "\t<li>7</li>\n",
       "\t<li>7.5</li>\n",
       "\t<li>8</li>\n",
       "\t<li>8.5</li>\n",
       "\t<li>9</li>\n",
       "\t<li>9.5</li>\n",
       "\t<li>10</li>\n",
       "\t<li>10.5</li>\n",
       "\t<li>11</li>\n",
       "\t<li>11.5</li>\n",
       "\t<li>12</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 1\n",
       "\\item 1.5\n",
       "\\item 2\n",
       "\\item 2.5\n",
       "\\item 3\n",
       "\\item 3.5\n",
       "\\item 4\n",
       "\\item 4.5\n",
       "\\item 5\n",
       "\\item 5.5\n",
       "\\item 6\n",
       "\\item 6.5\n",
       "\\item 7\n",
       "\\item 7.5\n",
       "\\item 8\n",
       "\\item 8.5\n",
       "\\item 9\n",
       "\\item 9.5\n",
       "\\item 10\n",
       "\\item 10.5\n",
       "\\item 11\n",
       "\\item 11.5\n",
       "\\item 12\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 1\n",
       "2. 1.5\n",
       "3. 2\n",
       "4. 2.5\n",
       "5. 3\n",
       "6. 3.5\n",
       "7. 4\n",
       "8. 4.5\n",
       "9. 5\n",
       "10. 5.5\n",
       "11. 6\n",
       "12. 6.5\n",
       "13. 7\n",
       "14. 7.5\n",
       "15. 8\n",
       "16. 8.5\n",
       "17. 9\n",
       "18. 9.5\n",
       "19. 10\n",
       "20. 10.5\n",
       "21. 11\n",
       "22. 11.5\n",
       "23. 12\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5  8.0\n",
       "[16]  8.5  9.0  9.5 10.0 10.5 11.0 11.5 12.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>1</li>\n",
       "\t<li>8</li>\n",
       "\t<li>27</li>\n",
       "\t<li>64</li>\n",
       "\t<li>125</li>\n",
       "\t<li>216</li>\n",
       "\t<li>343</li>\n",
       "\t<li>512</li>\n",
       "\t<li>729</li>\n",
       "\t<li>1000</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 1\n",
       "\\item 8\n",
       "\\item 27\n",
       "\\item 64\n",
       "\\item 125\n",
       "\\item 216\n",
       "\\item 343\n",
       "\\item 512\n",
       "\\item 729\n",
       "\\item 1000\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 1\n",
       "2. 8\n",
       "3. 27\n",
       "4. 64\n",
       "5. 125\n",
       "6. 216\n",
       "7. 343\n",
       "8. 512\n",
       "9. 729\n",
       "10. 1000\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1]    1    8   27   64  125  216  343  512  729 1000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>-1</li>\n",
       "\t<li>0.5</li>\n",
       "\t<li>-0.333333333333333</li>\n",
       "\t<li>0.25</li>\n",
       "\t<li>-0.2</li>\n",
       "\t<li>0.166666666666667</li>\n",
       "\t<li>-0.142857142857143</li>\n",
       "\t<li>0.125</li>\n",
       "\t<li>-0.111111111111111</li>\n",
       "\t<li>0.1</li>\n",
       "\t<li>-0.0909090909090909</li>\n",
       "\t<li>0.0833333333333333</li>\n",
       "\t<li>-0.0769230769230769</li>\n",
       "\t<li>0.0714285714285714</li>\n",
       "\t<li>-0.0666666666666667</li>\n",
       "\t<li>0.0625</li>\n",
       "\t<li>-0.0588235294117647</li>\n",
       "\t<li>0.0555555555555556</li>\n",
       "\t<li>-0.0526315789473684</li>\n",
       "\t<li>0.05</li>\n",
       "\t<li>-0.0476190476190476</li>\n",
       "\t<li>0.0454545454545455</li>\n",
       "\t<li>-0.0434782608695652</li>\n",
       "\t<li>0.0416666666666667</li>\n",
       "\t<li>-0.04</li>\n",
       "\t<li>0.0384615384615385</li>\n",
       "\t<li>-0.037037037037037</li>\n",
       "\t<li>0.0357142857142857</li>\n",
       "\t<li>-0.0344827586206897</li>\n",
       "\t<li>0.0333333333333333</li>\n",
       "\t<li>-0.032258064516129</li>\n",
       "\t<li>0.03125</li>\n",
       "\t<li>-0.0303030303030303</li>\n",
       "\t<li>0.0294117647058824</li>\n",
       "\t<li>-0.0285714285714286</li>\n",
       "\t<li>0.0277777777777778</li>\n",
       "\t<li>-0.027027027027027</li>\n",
       "\t<li>0.0263157894736842</li>\n",
       "\t<li>-0.0256410256410256</li>\n",
       "\t<li>0.025</li>\n",
       "\t<li>-0.024390243902439</li>\n",
       "\t<li>0.0238095238095238</li>\n",
       "\t<li>-0.0232558139534884</li>\n",
       "\t<li>0.0227272727272727</li>\n",
       "\t<li>-0.0222222222222222</li>\n",
       "\t<li>0.0217391304347826</li>\n",
       "\t<li>-0.0212765957446809</li>\n",
       "\t<li>0.0208333333333333</li>\n",
       "\t<li>-0.0204081632653061</li>\n",
       "\t<li>0.02</li>\n",
       "\t<li>-0.0196078431372549</li>\n",
       "\t<li>0.0192307692307692</li>\n",
       "\t<li>-0.0188679245283019</li>\n",
       "\t<li>0.0185185185185185</li>\n",
       "\t<li>-0.0181818181818182</li>\n",
       "\t<li>0.0178571428571429</li>\n",
       "\t<li>-0.0175438596491228</li>\n",
       "\t<li>0.0172413793103448</li>\n",
       "\t<li>-0.0169491525423729</li>\n",
       "\t<li>0.0166666666666667</li>\n",
       "\t<li>-0.0163934426229508</li>\n",
       "\t<li>0.0161290322580645</li>\n",
       "\t<li>-0.0158730158730159</li>\n",
       "\t<li>0.015625</li>\n",
       "\t<li>-0.0153846153846154</li>\n",
       "\t<li>0.0151515151515152</li>\n",
       "\t<li>-0.0149253731343284</li>\n",
       "\t<li>0.0147058823529412</li>\n",
       "\t<li>-0.0144927536231884</li>\n",
       "\t<li>0.0142857142857143</li>\n",
       "\t<li>-0.0140845070422535</li>\n",
       "\t<li>0.0138888888888889</li>\n",
       "\t<li>-0.0136986301369863</li>\n",
       "\t<li>0.0135135135135135</li>\n",
       "\t<li>-0.0133333333333333</li>\n",
       "\t<li>0.0131578947368421</li>\n",
       "\t<li>-0.012987012987013</li>\n",
       "\t<li>0.0128205128205128</li>\n",
       "\t<li>-0.0126582278481013</li>\n",
       "\t<li>0.0125</li>\n",
       "\t<li>-0.0123456790123457</li>\n",
       "\t<li>0.0121951219512195</li>\n",
       "\t<li>-0.0120481927710843</li>\n",
       "\t<li>0.0119047619047619</li>\n",
       "\t<li>-0.0117647058823529</li>\n",
       "\t<li>0.0116279069767442</li>\n",
       "\t<li>-0.0114942528735632</li>\n",
       "\t<li>0.0113636363636364</li>\n",
       "\t<li>-0.0112359550561798</li>\n",
       "\t<li>0.0111111111111111</li>\n",
       "\t<li>-0.010989010989011</li>\n",
       "\t<li>0.0108695652173913</li>\n",
       "\t<li>-0.010752688172043</li>\n",
       "\t<li>0.0106382978723404</li>\n",
       "\t<li>-0.0105263157894737</li>\n",
       "\t<li>0.0104166666666667</li>\n",
       "\t<li>-0.0103092783505155</li>\n",
       "\t<li>0.0102040816326531</li>\n",
       "\t<li>-0.0101010101010101</li>\n",
       "\t<li>0.01</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item -1\n",
       "\\item 0.5\n",
       "\\item -0.333333333333333\n",
       "\\item 0.25\n",
       "\\item -0.2\n",
       "\\item 0.166666666666667\n",
       "\\item -0.142857142857143\n",
       "\\item 0.125\n",
       "\\item -0.111111111111111\n",
       "\\item 0.1\n",
       "\\item -0.0909090909090909\n",
       "\\item 0.0833333333333333\n",
       "\\item -0.0769230769230769\n",
       "\\item 0.0714285714285714\n",
       "\\item -0.0666666666666667\n",
       "\\item 0.0625\n",
       "\\item -0.0588235294117647\n",
       "\\item 0.0555555555555556\n",
       "\\item -0.0526315789473684\n",
       "\\item 0.05\n",
       "\\item -0.0476190476190476\n",
       "\\item 0.0454545454545455\n",
       "\\item -0.0434782608695652\n",
       "\\item 0.0416666666666667\n",
       "\\item -0.04\n",
       "\\item 0.0384615384615385\n",
       "\\item -0.037037037037037\n",
       "\\item 0.0357142857142857\n",
       "\\item -0.0344827586206897\n",
       "\\item 0.0333333333333333\n",
       "\\item -0.032258064516129\n",
       "\\item 0.03125\n",
       "\\item -0.0303030303030303\n",
       "\\item 0.0294117647058824\n",
       "\\item -0.0285714285714286\n",
       "\\item 0.0277777777777778\n",
       "\\item -0.027027027027027\n",
       "\\item 0.0263157894736842\n",
       "\\item -0.0256410256410256\n",
       "\\item 0.025\n",
       "\\item -0.024390243902439\n",
       "\\item 0.0238095238095238\n",
       "\\item -0.0232558139534884\n",
       "\\item 0.0227272727272727\n",
       "\\item -0.0222222222222222\n",
       "\\item 0.0217391304347826\n",
       "\\item -0.0212765957446809\n",
       "\\item 0.0208333333333333\n",
       "\\item -0.0204081632653061\n",
       "\\item 0.02\n",
       "\\item -0.0196078431372549\n",
       "\\item 0.0192307692307692\n",
       "\\item -0.0188679245283019\n",
       "\\item 0.0185185185185185\n",
       "\\item -0.0181818181818182\n",
       "\\item 0.0178571428571429\n",
       "\\item -0.0175438596491228\n",
       "\\item 0.0172413793103448\n",
       "\\item -0.0169491525423729\n",
       "\\item 0.0166666666666667\n",
       "\\item -0.0163934426229508\n",
       "\\item 0.0161290322580645\n",
       "\\item -0.0158730158730159\n",
       "\\item 0.015625\n",
       "\\item -0.0153846153846154\n",
       "\\item 0.0151515151515152\n",
       "\\item -0.0149253731343284\n",
       "\\item 0.0147058823529412\n",
       "\\item -0.0144927536231884\n",
       "\\item 0.0142857142857143\n",
       "\\item -0.0140845070422535\n",
       "\\item 0.0138888888888889\n",
       "\\item -0.0136986301369863\n",
       "\\item 0.0135135135135135\n",
       "\\item -0.0133333333333333\n",
       "\\item 0.0131578947368421\n",
       "\\item -0.012987012987013\n",
       "\\item 0.0128205128205128\n",
       "\\item -0.0126582278481013\n",
       "\\item 0.0125\n",
       "\\item -0.0123456790123457\n",
       "\\item 0.0121951219512195\n",
       "\\item -0.0120481927710843\n",
       "\\item 0.0119047619047619\n",
       "\\item -0.0117647058823529\n",
       "\\item 0.0116279069767442\n",
       "\\item -0.0114942528735632\n",
       "\\item 0.0113636363636364\n",
       "\\item -0.0112359550561798\n",
       "\\item 0.0111111111111111\n",
       "\\item -0.010989010989011\n",
       "\\item 0.0108695652173913\n",
       "\\item -0.010752688172043\n",
       "\\item 0.0106382978723404\n",
       "\\item -0.0105263157894737\n",
       "\\item 0.0104166666666667\n",
       "\\item -0.0103092783505155\n",
       "\\item 0.0102040816326531\n",
       "\\item -0.0101010101010101\n",
       "\\item 0.01\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. -1\n",
       "2. 0.5\n",
       "3. -0.333333333333333\n",
       "4. 0.25\n",
       "5. -0.2\n",
       "6. 0.166666666666667\n",
       "7. -0.142857142857143\n",
       "8. 0.125\n",
       "9. -0.111111111111111\n",
       "10. 0.1\n",
       "11. -0.0909090909090909\n",
       "12. 0.0833333333333333\n",
       "13. -0.0769230769230769\n",
       "14. 0.0714285714285714\n",
       "15. -0.0666666666666667\n",
       "16. 0.0625\n",
       "17. -0.0588235294117647\n",
       "18. 0.0555555555555556\n",
       "19. -0.0526315789473684\n",
       "20. 0.05\n",
       "21. -0.0476190476190476\n",
       "22. 0.0454545454545455\n",
       "23. -0.0434782608695652\n",
       "24. 0.0416666666666667\n",
       "25. -0.04\n",
       "26. 0.0384615384615385\n",
       "27. -0.037037037037037\n",
       "28. 0.0357142857142857\n",
       "29. -0.0344827586206897\n",
       "30. 0.0333333333333333\n",
       "31. -0.032258064516129\n",
       "32. 0.03125\n",
       "33. -0.0303030303030303\n",
       "34. 0.0294117647058824\n",
       "35. -0.0285714285714286\n",
       "36. 0.0277777777777778\n",
       "37. -0.027027027027027\n",
       "38. 0.0263157894736842\n",
       "39. -0.0256410256410256\n",
       "40. 0.025\n",
       "41. -0.024390243902439\n",
       "42. 0.0238095238095238\n",
       "43. -0.0232558139534884\n",
       "44. 0.0227272727272727\n",
       "45. -0.0222222222222222\n",
       "46. 0.0217391304347826\n",
       "47. -0.0212765957446809\n",
       "48. 0.0208333333333333\n",
       "49. -0.0204081632653061\n",
       "50. 0.02\n",
       "51. -0.0196078431372549\n",
       "52. 0.0192307692307692\n",
       "53. -0.0188679245283019\n",
       "54. 0.0185185185185185\n",
       "55. -0.0181818181818182\n",
       "56. 0.0178571428571429\n",
       "57. -0.0175438596491228\n",
       "58. 0.0172413793103448\n",
       "59. -0.0169491525423729\n",
       "60. 0.0166666666666667\n",
       "61. -0.0163934426229508\n",
       "62. 0.0161290322580645\n",
       "63. -0.0158730158730159\n",
       "64. 0.015625\n",
       "65. -0.0153846153846154\n",
       "66. 0.0151515151515152\n",
       "67. -0.0149253731343284\n",
       "68. 0.0147058823529412\n",
       "69. -0.0144927536231884\n",
       "70. 0.0142857142857143\n",
       "71. -0.0140845070422535\n",
       "72. 0.0138888888888889\n",
       "73. -0.0136986301369863\n",
       "74. 0.0135135135135135\n",
       "75. -0.0133333333333333\n",
       "76. 0.0131578947368421\n",
       "77. -0.012987012987013\n",
       "78. 0.0128205128205128\n",
       "79. -0.0126582278481013\n",
       "80. 0.0125\n",
       "81. -0.0123456790123457\n",
       "82. 0.0121951219512195\n",
       "83. -0.0120481927710843\n",
       "84. 0.0119047619047619\n",
       "85. -0.0117647058823529\n",
       "86. 0.0116279069767442\n",
       "87. -0.0114942528735632\n",
       "88. 0.0113636363636364\n",
       "89. -0.0112359550561798\n",
       "90. 0.0111111111111111\n",
       "91. -0.010989010989011\n",
       "92. 0.0108695652173913\n",
       "93. -0.010752688172043\n",
       "94. 0.0106382978723404\n",
       "95. -0.0105263157894737\n",
       "96. 0.0104166666666667\n",
       "97. -0.0103092783505155\n",
       "98. 0.0102040816326531\n",
       "99. -0.0101010101010101\n",
       "100. 0.01\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "  [1] -1.00000000  0.50000000 -0.33333333  0.25000000 -0.20000000  0.16666667\n",
       "  [7] -0.14285714  0.12500000 -0.11111111  0.10000000 -0.09090909  0.08333333\n",
       " [13] -0.07692308  0.07142857 -0.06666667  0.06250000 -0.05882353  0.05555556\n",
       " [19] -0.05263158  0.05000000 -0.04761905  0.04545455 -0.04347826  0.04166667\n",
       " [25] -0.04000000  0.03846154 -0.03703704  0.03571429 -0.03448276  0.03333333\n",
       " [31] -0.03225806  0.03125000 -0.03030303  0.02941176 -0.02857143  0.02777778\n",
       " [37] -0.02702703  0.02631579 -0.02564103  0.02500000 -0.02439024  0.02380952\n",
       " [43] -0.02325581  0.02272727 -0.02222222  0.02173913 -0.02127660  0.02083333\n",
       " [49] -0.02040816  0.02000000 -0.01960784  0.01923077 -0.01886792  0.01851852\n",
       " [55] -0.01818182  0.01785714 -0.01754386  0.01724138 -0.01694915  0.01666667\n",
       " [61] -0.01639344  0.01612903 -0.01587302  0.01562500 -0.01538462  0.01515152\n",
       " [67] -0.01492537  0.01470588 -0.01449275  0.01428571 -0.01408451  0.01388889\n",
       " [73] -0.01369863  0.01351351 -0.01333333  0.01315789 -0.01298701  0.01282051\n",
       " [79] -0.01265823  0.01250000 -0.01234568  0.01219512 -0.01204819  0.01190476\n",
       " [85] -0.01176471  0.01162791 -0.01149425  0.01136364 -0.01123596  0.01111111\n",
       " [91] -0.01098901  0.01086957 -0.01075269  0.01063830 -0.01052632  0.01041667\n",
       " [97] -0.01030928  0.01020408 -0.01010101  0.01000000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>1</li>\n",
       "\t<li>3</li>\n",
       "\t<li>6</li>\n",
       "\t<li>10</li>\n",
       "\t<li>15</li>\n",
       "\t<li>21</li>\n",
       "\t<li>28</li>\n",
       "\t<li>36</li>\n",
       "\t<li>45</li>\n",
       "\t<li>55</li>\n",
       "\t<li>66</li>\n",
       "\t<li>78</li>\n",
       "\t<li>91</li>\n",
       "\t<li>105</li>\n",
       "\t<li>120</li>\n",
       "\t<li>136</li>\n",
       "\t<li>153</li>\n",
       "\t<li>171</li>\n",
       "\t<li>190</li>\n",
       "\t<li>210</li>\n",
       "\t<li>231</li>\n",
       "\t<li>253</li>\n",
       "\t<li>276</li>\n",
       "\t<li>300</li>\n",
       "\t<li>325</li>\n",
       "\t<li>351</li>\n",
       "\t<li>378</li>\n",
       "\t<li>406</li>\n",
       "\t<li>435</li>\n",
       "\t<li>465</li>\n",
       "\t<li>496</li>\n",
       "\t<li>528</li>\n",
       "\t<li>561</li>\n",
       "\t<li>595</li>\n",
       "\t<li>630</li>\n",
       "\t<li>666</li>\n",
       "\t<li>703</li>\n",
       "\t<li>741</li>\n",
       "\t<li>780</li>\n",
       "\t<li>820</li>\n",
       "\t<li>861</li>\n",
       "\t<li>903</li>\n",
       "\t<li>946</li>\n",
       "\t<li>990</li>\n",
       "\t<li>1035</li>\n",
       "\t<li>1081</li>\n",
       "\t<li>1128</li>\n",
       "\t<li>1176</li>\n",
       "\t<li>1225</li>\n",
       "\t<li>1275</li>\n",
       "\t<li>1326</li>\n",
       "\t<li>1378</li>\n",
       "\t<li>1431</li>\n",
       "\t<li>1485</li>\n",
       "\t<li>1540</li>\n",
       "\t<li>1596</li>\n",
       "\t<li>1653</li>\n",
       "\t<li>1711</li>\n",
       "\t<li>1770</li>\n",
       "\t<li>1830</li>\n",
       "\t<li>1891</li>\n",
       "\t<li>1953</li>\n",
       "\t<li>2016</li>\n",
       "\t<li>2080</li>\n",
       "\t<li>2145</li>\n",
       "\t<li>2211</li>\n",
       "\t<li>2278</li>\n",
       "\t<li>2346</li>\n",
       "\t<li>2415</li>\n",
       "\t<li>2485</li>\n",
       "\t<li>2556</li>\n",
       "\t<li>2628</li>\n",
       "\t<li>2701</li>\n",
       "\t<li>2775</li>\n",
       "\t<li>2850</li>\n",
       "\t<li>2926</li>\n",
       "\t<li>3003</li>\n",
       "\t<li>3081</li>\n",
       "\t<li>3160</li>\n",
       "\t<li>3240</li>\n",
       "\t<li>3321</li>\n",
       "\t<li>3403</li>\n",
       "\t<li>3486</li>\n",
       "\t<li>3570</li>\n",
       "\t<li>3655</li>\n",
       "\t<li>3741</li>\n",
       "\t<li>3828</li>\n",
       "\t<li>3916</li>\n",
       "\t<li>4005</li>\n",
       "\t<li>4095</li>\n",
       "\t<li>4186</li>\n",
       "\t<li>4278</li>\n",
       "\t<li>4371</li>\n",
       "\t<li>4465</li>\n",
       "\t<li>4560</li>\n",
       "\t<li>4656</li>\n",
       "\t<li>4753</li>\n",
       "\t<li>4851</li>\n",
       "\t<li>4950</li>\n",
       "\t<li>5050</li>\n",
       "\t<li>5151</li>\n",
       "\t<li>5253</li>\n",
       "\t<li>5356</li>\n",
       "\t<li>5460</li>\n",
       "\t<li>5565</li>\n",
       "\t<li>5671</li>\n",
       "\t<li>5778</li>\n",
       "\t<li>5886</li>\n",
       "\t<li>5995</li>\n",
       "\t<li>6105</li>\n",
       "\t<li>6216</li>\n",
       "\t<li>6328</li>\n",
       "\t<li>6441</li>\n",
       "\t<li>6555</li>\n",
       "\t<li>6670</li>\n",
       "\t<li>6786</li>\n",
       "\t<li>6903</li>\n",
       "\t<li>7021</li>\n",
       "\t<li>7140</li>\n",
       "\t<li>7260</li>\n",
       "\t<li>7381</li>\n",
       "\t<li>7503</li>\n",
       "\t<li>7626</li>\n",
       "\t<li>7750</li>\n",
       "\t<li>7875</li>\n",
       "\t<li>8001</li>\n",
       "\t<li>8128</li>\n",
       "\t<li>8256</li>\n",
       "\t<li>8385</li>\n",
       "\t<li>8515</li>\n",
       "\t<li>8646</li>\n",
       "\t<li>8778</li>\n",
       "\t<li>8911</li>\n",
       "\t<li>9045</li>\n",
       "\t<li>9180</li>\n",
       "\t<li>9316</li>\n",
       "\t<li>9453</li>\n",
       "\t<li>9591</li>\n",
       "\t<li>9730</li>\n",
       "\t<li>9870</li>\n",
       "\t<li>10011</li>\n",
       "\t<li>10153</li>\n",
       "\t<li>10296</li>\n",
       "\t<li>10440</li>\n",
       "\t<li>10585</li>\n",
       "\t<li>10731</li>\n",
       "\t<li>10878</li>\n",
       "\t<li>11026</li>\n",
       "\t<li>11175</li>\n",
       "\t<li>11325</li>\n",
       "\t<li>11476</li>\n",
       "\t<li>11628</li>\n",
       "\t<li>11781</li>\n",
       "\t<li>11935</li>\n",
       "\t<li>12090</li>\n",
       "\t<li>12246</li>\n",
       "\t<li>12403</li>\n",
       "\t<li>12561</li>\n",
       "\t<li>12720</li>\n",
       "\t<li>12880</li>\n",
       "\t<li>13041</li>\n",
       "\t<li>13203</li>\n",
       "\t<li>13366</li>\n",
       "\t<li>13530</li>\n",
       "\t<li>13695</li>\n",
       "\t<li>13861</li>\n",
       "\t<li>14028</li>\n",
       "\t<li>14196</li>\n",
       "\t<li>14365</li>\n",
       "\t<li>14535</li>\n",
       "\t<li>14706</li>\n",
       "\t<li>14878</li>\n",
       "\t<li>15051</li>\n",
       "\t<li>15225</li>\n",
       "\t<li>15400</li>\n",
       "\t<li>15576</li>\n",
       "\t<li>15753</li>\n",
       "\t<li>15931</li>\n",
       "\t<li>16110</li>\n",
       "\t<li>16290</li>\n",
       "\t<li>16471</li>\n",
       "\t<li>16653</li>\n",
       "\t<li>16836</li>\n",
       "\t<li>17020</li>\n",
       "\t<li>17205</li>\n",
       "\t<li>17391</li>\n",
       "\t<li>17578</li>\n",
       "\t<li>17766</li>\n",
       "\t<li>17955</li>\n",
       "\t<li>18145</li>\n",
       "\t<li>18336</li>\n",
       "\t<li>18528</li>\n",
       "\t<li>18721</li>\n",
       "\t<li>18915</li>\n",
       "\t<li>19110</li>\n",
       "\t<li>19306</li>\n",
       "\t<li>19503</li>\n",
       "\t<li>19701</li>\n",
       "\t<li>19900</li>\n",
       "\t<li>20100</li>\n",
       "\t<li>20301</li>\n",
       "\t<li>20503</li>\n",
       "\t<li>20706</li>\n",
       "\t<li>20910</li>\n",
       "\t<li>21115</li>\n",
       "\t<li>21321</li>\n",
       "\t<li>21528</li>\n",
       "\t<li>21736</li>\n",
       "\t<li>21945</li>\n",
       "\t<li>22155</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 1\n",
       "\\item 3\n",
       "\\item 6\n",
       "\\item 10\n",
       "\\item 15\n",
       "\\item 21\n",
       "\\item 28\n",
       "\\item 36\n",
       "\\item 45\n",
       "\\item 55\n",
       "\\item 66\n",
       "\\item 78\n",
       "\\item 91\n",
       "\\item 105\n",
       "\\item 120\n",
       "\\item 136\n",
       "\\item 153\n",
       "\\item 171\n",
       "\\item 190\n",
       "\\item 210\n",
       "\\item 231\n",
       "\\item 253\n",
       "\\item 276\n",
       "\\item 300\n",
       "\\item 325\n",
       "\\item 351\n",
       "\\item 378\n",
       "\\item 406\n",
       "\\item 435\n",
       "\\item 465\n",
       "\\item 496\n",
       "\\item 528\n",
       "\\item 561\n",
       "\\item 595\n",
       "\\item 630\n",
       "\\item 666\n",
       "\\item 703\n",
       "\\item 741\n",
       "\\item 780\n",
       "\\item 820\n",
       "\\item 861\n",
       "\\item 903\n",
       "\\item 946\n",
       "\\item 990\n",
       "\\item 1035\n",
       "\\item 1081\n",
       "\\item 1128\n",
       "\\item 1176\n",
       "\\item 1225\n",
       "\\item 1275\n",
       "\\item 1326\n",
       "\\item 1378\n",
       "\\item 1431\n",
       "\\item 1485\n",
       "\\item 1540\n",
       "\\item 1596\n",
       "\\item 1653\n",
       "\\item 1711\n",
       "\\item 1770\n",
       "\\item 1830\n",
       "\\item 1891\n",
       "\\item 1953\n",
       "\\item 2016\n",
       "\\item 2080\n",
       "\\item 2145\n",
       "\\item 2211\n",
       "\\item 2278\n",
       "\\item 2346\n",
       "\\item 2415\n",
       "\\item 2485\n",
       "\\item 2556\n",
       "\\item 2628\n",
       "\\item 2701\n",
       "\\item 2775\n",
       "\\item 2850\n",
       "\\item 2926\n",
       "\\item 3003\n",
       "\\item 3081\n",
       "\\item 3160\n",
       "\\item 3240\n",
       "\\item 3321\n",
       "\\item 3403\n",
       "\\item 3486\n",
       "\\item 3570\n",
       "\\item 3655\n",
       "\\item 3741\n",
       "\\item 3828\n",
       "\\item 3916\n",
       "\\item 4005\n",
       "\\item 4095\n",
       "\\item 4186\n",
       "\\item 4278\n",
       "\\item 4371\n",
       "\\item 4465\n",
       "\\item 4560\n",
       "\\item 4656\n",
       "\\item 4753\n",
       "\\item 4851\n",
       "\\item 4950\n",
       "\\item 5050\n",
       "\\item 5151\n",
       "\\item 5253\n",
       "\\item 5356\n",
       "\\item 5460\n",
       "\\item 5565\n",
       "\\item 5671\n",
       "\\item 5778\n",
       "\\item 5886\n",
       "\\item 5995\n",
       "\\item 6105\n",
       "\\item 6216\n",
       "\\item 6328\n",
       "\\item 6441\n",
       "\\item 6555\n",
       "\\item 6670\n",
       "\\item 6786\n",
       "\\item 6903\n",
       "\\item 7021\n",
       "\\item 7140\n",
       "\\item 7260\n",
       "\\item 7381\n",
       "\\item 7503\n",
       "\\item 7626\n",
       "\\item 7750\n",
       "\\item 7875\n",
       "\\item 8001\n",
       "\\item 8128\n",
       "\\item 8256\n",
       "\\item 8385\n",
       "\\item 8515\n",
       "\\item 8646\n",
       "\\item 8778\n",
       "\\item 8911\n",
       "\\item 9045\n",
       "\\item 9180\n",
       "\\item 9316\n",
       "\\item 9453\n",
       "\\item 9591\n",
       "\\item 9730\n",
       "\\item 9870\n",
       "\\item 10011\n",
       "\\item 10153\n",
       "\\item 10296\n",
       "\\item 10440\n",
       "\\item 10585\n",
       "\\item 10731\n",
       "\\item 10878\n",
       "\\item 11026\n",
       "\\item 11175\n",
       "\\item 11325\n",
       "\\item 11476\n",
       "\\item 11628\n",
       "\\item 11781\n",
       "\\item 11935\n",
       "\\item 12090\n",
       "\\item 12246\n",
       "\\item 12403\n",
       "\\item 12561\n",
       "\\item 12720\n",
       "\\item 12880\n",
       "\\item 13041\n",
       "\\item 13203\n",
       "\\item 13366\n",
       "\\item 13530\n",
       "\\item 13695\n",
       "\\item 13861\n",
       "\\item 14028\n",
       "\\item 14196\n",
       "\\item 14365\n",
       "\\item 14535\n",
       "\\item 14706\n",
       "\\item 14878\n",
       "\\item 15051\n",
       "\\item 15225\n",
       "\\item 15400\n",
       "\\item 15576\n",
       "\\item 15753\n",
       "\\item 15931\n",
       "\\item 16110\n",
       "\\item 16290\n",
       "\\item 16471\n",
       "\\item 16653\n",
       "\\item 16836\n",
       "\\item 17020\n",
       "\\item 17205\n",
       "\\item 17391\n",
       "\\item 17578\n",
       "\\item 17766\n",
       "\\item 17955\n",
       "\\item 18145\n",
       "\\item 18336\n",
       "\\item 18528\n",
       "\\item 18721\n",
       "\\item 18915\n",
       "\\item 19110\n",
       "\\item 19306\n",
       "\\item 19503\n",
       "\\item 19701\n",
       "\\item 19900\n",
       "\\item 20100\n",
       "\\item 20301\n",
       "\\item 20503\n",
       "\\item 20706\n",
       "\\item 20910\n",
       "\\item 21115\n",
       "\\item 21321\n",
       "\\item 21528\n",
       "\\item 21736\n",
       "\\item 21945\n",
       "\\item 22155\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 1\n",
       "2. 3\n",
       "3. 6\n",
       "4. 10\n",
       "5. 15\n",
       "6. 21\n",
       "7. 28\n",
       "8. 36\n",
       "9. 45\n",
       "10. 55\n",
       "11. 66\n",
       "12. 78\n",
       "13. 91\n",
       "14. 105\n",
       "15. 120\n",
       "16. 136\n",
       "17. 153\n",
       "18. 171\n",
       "19. 190\n",
       "20. 210\n",
       "21. 231\n",
       "22. 253\n",
       "23. 276\n",
       "24. 300\n",
       "25. 325\n",
       "26. 351\n",
       "27. 378\n",
       "28. 406\n",
       "29. 435\n",
       "30. 465\n",
       "31. 496\n",
       "32. 528\n",
       "33. 561\n",
       "34. 595\n",
       "35. 630\n",
       "36. 666\n",
       "37. 703\n",
       "38. 741\n",
       "39. 780\n",
       "40. 820\n",
       "41. 861\n",
       "42. 903\n",
       "43. 946\n",
       "44. 990\n",
       "45. 1035\n",
       "46. 1081\n",
       "47. 1128\n",
       "48. 1176\n",
       "49. 1225\n",
       "50. 1275\n",
       "51. 1326\n",
       "52. 1378\n",
       "53. 1431\n",
       "54. 1485\n",
       "55. 1540\n",
       "56. 1596\n",
       "57. 1653\n",
       "58. 1711\n",
       "59. 1770\n",
       "60. 1830\n",
       "61. 1891\n",
       "62. 1953\n",
       "63. 2016\n",
       "64. 2080\n",
       "65. 2145\n",
       "66. 2211\n",
       "67. 2278\n",
       "68. 2346\n",
       "69. 2415\n",
       "70. 2485\n",
       "71. 2556\n",
       "72. 2628\n",
       "73. 2701\n",
       "74. 2775\n",
       "75. 2850\n",
       "76. 2926\n",
       "77. 3003\n",
       "78. 3081\n",
       "79. 3160\n",
       "80. 3240\n",
       "81. 3321\n",
       "82. 3403\n",
       "83. 3486\n",
       "84. 3570\n",
       "85. 3655\n",
       "86. 3741\n",
       "87. 3828\n",
       "88. 3916\n",
       "89. 4005\n",
       "90. 4095\n",
       "91. 4186\n",
       "92. 4278\n",
       "93. 4371\n",
       "94. 4465\n",
       "95. 4560\n",
       "96. 4656\n",
       "97. 4753\n",
       "98. 4851\n",
       "99. 4950\n",
       "100. 5050\n",
       "101. 5151\n",
       "102. 5253\n",
       "103. 5356\n",
       "104. 5460\n",
       "105. 5565\n",
       "106. 5671\n",
       "107. 5778\n",
       "108. 5886\n",
       "109. 5995\n",
       "110. 6105\n",
       "111. 6216\n",
       "112. 6328\n",
       "113. 6441\n",
       "114. 6555\n",
       "115. 6670\n",
       "116. 6786\n",
       "117. 6903\n",
       "118. 7021\n",
       "119. 7140\n",
       "120. 7260\n",
       "121. 7381\n",
       "122. 7503\n",
       "123. 7626\n",
       "124. 7750\n",
       "125. 7875\n",
       "126. 8001\n",
       "127. 8128\n",
       "128. 8256\n",
       "129. 8385\n",
       "130. 8515\n",
       "131. 8646\n",
       "132. 8778\n",
       "133. 8911\n",
       "134. 9045\n",
       "135. 9180\n",
       "136. 9316\n",
       "137. 9453\n",
       "138. 9591\n",
       "139. 9730\n",
       "140. 9870\n",
       "141. 10011\n",
       "142. 10153\n",
       "143. 10296\n",
       "144. 10440\n",
       "145. 10585\n",
       "146. 10731\n",
       "147. 10878\n",
       "148. 11026\n",
       "149. 11175\n",
       "150. 11325\n",
       "151. 11476\n",
       "152. 11628\n",
       "153. 11781\n",
       "154. 11935\n",
       "155. 12090\n",
       "156. 12246\n",
       "157. 12403\n",
       "158. 12561\n",
       "159. 12720\n",
       "160. 12880\n",
       "161. 13041\n",
       "162. 13203\n",
       "163. 13366\n",
       "164. 13530\n",
       "165. 13695\n",
       "166. 13861\n",
       "167. 14028\n",
       "168. 14196\n",
       "169. 14365\n",
       "170. 14535\n",
       "171. 14706\n",
       "172. 14878\n",
       "173. 15051\n",
       "174. 15225\n",
       "175. 15400\n",
       "176. 15576\n",
       "177. 15753\n",
       "178. 15931\n",
       "179. 16110\n",
       "180. 16290\n",
       "181. 16471\n",
       "182. 16653\n",
       "183. 16836\n",
       "184. 17020\n",
       "185. 17205\n",
       "186. 17391\n",
       "187. 17578\n",
       "188. 17766\n",
       "189. 17955\n",
       "190. 18145\n",
       "191. 18336\n",
       "192. 18528\n",
       "193. 18721\n",
       "194. 18915\n",
       "195. 19110\n",
       "196. 19306\n",
       "197. 19503\n",
       "198. 19701\n",
       "199. 19900\n",
       "200. 20100\n",
       "201. 20301\n",
       "202. 20503\n",
       "203. 20706\n",
       "204. 20910\n",
       "205. 21115\n",
       "206. 21321\n",
       "207. 21528\n",
       "208. 21736\n",
       "209. 21945\n",
       "210. 22155\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "  [1]     1     3     6    10    15    21    28    36    45    55    66    78\n",
       " [13]    91   105   120   136   153   171   190   210   231   253   276   300\n",
       " [25]   325   351   378   406   435   465   496   528   561   595   630   666\n",
       " [37]   703   741   780   820   861   903   946   990  1035  1081  1128  1176\n",
       " [49]  1225  1275  1326  1378  1431  1485  1540  1596  1653  1711  1770  1830\n",
       " [61]  1891  1953  2016  2080  2145  2211  2278  2346  2415  2485  2556  2628\n",
       " [73]  2701  2775  2850  2926  3003  3081  3160  3240  3321  3403  3486  3570\n",
       " [85]  3655  3741  3828  3916  4005  4095  4186  4278  4371  4465  4560  4656\n",
       " [97]  4753  4851  4950  5050  5151  5253  5356  5460  5565  5671  5778  5886\n",
       "[109]  5995  6105  6216  6328  6441  6555  6670  6786  6903  7021  7140  7260\n",
       "[121]  7381  7503  7626  7750  7875  8001  8128  8256  8385  8515  8646  8778\n",
       "[133]  8911  9045  9180  9316  9453  9591  9730  9870 10011 10153 10296 10440\n",
       "[145] 10585 10731 10878 11026 11175 11325 11476 11628 11781 11935 12090 12246\n",
       "[157] 12403 12561 12720 12880 13041 13203 13366 13530 13695 13861 14028 14196\n",
       "[169] 14365 14535 14706 14878 15051 15225 15400 15576 15753 15931 16110 16290\n",
       "[181] 16471 16653 16836 17020 17205 17391 17578 17766 17955 18145 18336 18528\n",
       "[193] 18721 18915 19110 19306 19503 19701 19900 20100 20301 20503 20706 20910\n",
       "[205] 21115 21321 21528 21736 21945 22155"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#(i)\n",
    "seq(from = 1,to = 12, by = 0.5)\n",
    "#(ii)\n",
    "1:10 * seq(from = 1,to = 10, by = 1)^2\n",
    "#(iii)\n",
    "1/(1:100) * (-1)^(1:100)\n",
    "#(iv)\n",
    "cumsum(1:210)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 子集\n",
    "\n",
    "我們经常需要提取向量中的一些元素。 在R中，您可以使用**方括号**来选择单个元素或元素组：<br>\n",
    "```R\n",
    "> x <- c(5,9,2,14,-4)\n",
    "\n",
    "> x[3]\n",
    "\n",
    "[1] 2\n",
    "\n",
    "> # note indexing starts from 1\n",
    "> x[c(2,3,5)] [1] 9 2-4 > x[1:3]\n",
    "\n",
    "[1] 5 9 2\n",
    "\n",
    "> x[3:length(x)]\n",
    "\n",
    "[1]  2 14 -4\n",
    "```\n",
    "獲取子向量還有兩種方法，第一种是**比較向量的大小，然後給出布爾值**（即包含TRUE和FALSE）：\n",
    "```R\n",
    "> x>4 # to judge if x > 4 \n",
    "\n",
    "[1] TRUE TRUE FALSE TRUE FALSE > x[x > 4]\n",
    "\n",
    "[1] 5 9 14\n",
    "```\n",
    "或者使用**負數索引**來指定不應該選擇的元素：\n",
    "```R\n",
    "> x[-1]\n",
    "[1] 9 214-4\n",
    "\n",
    "> x[-c(1,4)] \n",
    "\n",
    "[1] 9 2-4\n",
    "``` \n",
    "*這種**負數索引**等於把剔除掉代表的 `x[i]` ；另，這與其他語言中（例如 C 和 python ）的負面索引解釋有這很大的不同）*\n",
    "<br>\n",
    "#### Exercise 2.4.\n",
    "The built-in vector LETTERS contains the uppercase letters of the alphabet. Produce a vector of <br>\n",
    "(i) the first 12 letters; <br>\n",
    "(ii) the odd ‘numbered’ letters; <br>\n",
    "(iii) the (English) consonants.\n",
    "<br>\n",
    "**Answer**\n",
    "```R\n",
    "#(i)\n",
    "> LETTERS[(1:12)]\n",
    " [1] \"A\" \"B\" \"C\" \"D\" \"E\" \"F\" \"G\" \"H\" \"I\" \"J\" \"K\" \"L\"\n",
    "#(ii)\n",
    "> LETTERS[seq(from = 1, to = 26, by = 2)]\n",
    " [1] \"A\" \"C\" \"E\" \"G\" \"I\" \"K\" \"M\" \"O\" \"Q\" \"S\" \"U\" \"W\" \"Y\"\n",
    "#(iii)\n",
    "> LETTERS[c(-1, -5, -9, -15, -21)]\n",
    " [1] \"B\" \"C\" \"D\" \"F\" \"G\" \"H\" \"J\" \"K\" \"L\" \"M\" \"N\" \"P\" \"Q\" \"R\" \"S\" \"T\" \"V\" \"W\"\n",
    "[19] \"X\" \"Y\" \"Z\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'A'</li>\n",
       "\t<li>'B'</li>\n",
       "\t<li>'C'</li>\n",
       "\t<li>'D'</li>\n",
       "\t<li>'E'</li>\n",
       "\t<li>'F'</li>\n",
       "\t<li>'G'</li>\n",
       "\t<li>'H'</li>\n",
       "\t<li>'I'</li>\n",
       "\t<li>'J'</li>\n",
       "\t<li>'K'</li>\n",
       "\t<li>'L'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'A'\n",
       "\\item 'B'\n",
       "\\item 'C'\n",
       "\\item 'D'\n",
       "\\item 'E'\n",
       "\\item 'F'\n",
       "\\item 'G'\n",
       "\\item 'H'\n",
       "\\item 'I'\n",
       "\\item 'J'\n",
       "\\item 'K'\n",
       "\\item 'L'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'A'\n",
       "2. 'B'\n",
       "3. 'C'\n",
       "4. 'D'\n",
       "5. 'E'\n",
       "6. 'F'\n",
       "7. 'G'\n",
       "8. 'H'\n",
       "9. 'I'\n",
       "10. 'J'\n",
       "11. 'K'\n",
       "12. 'L'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] \"A\" \"B\" \"C\" \"D\" \"E\" \"F\" \"G\" \"H\" \"I\" \"J\" \"K\" \"L\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'A'</li>\n",
       "\t<li>'C'</li>\n",
       "\t<li>'E'</li>\n",
       "\t<li>'G'</li>\n",
       "\t<li>'I'</li>\n",
       "\t<li>'K'</li>\n",
       "\t<li>'M'</li>\n",
       "\t<li>'O'</li>\n",
       "\t<li>'Q'</li>\n",
       "\t<li>'S'</li>\n",
       "\t<li>'U'</li>\n",
       "\t<li>'W'</li>\n",
       "\t<li>'Y'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'A'\n",
       "\\item 'C'\n",
       "\\item 'E'\n",
       "\\item 'G'\n",
       "\\item 'I'\n",
       "\\item 'K'\n",
       "\\item 'M'\n",
       "\\item 'O'\n",
       "\\item 'Q'\n",
       "\\item 'S'\n",
       "\\item 'U'\n",
       "\\item 'W'\n",
       "\\item 'Y'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'A'\n",
       "2. 'C'\n",
       "3. 'E'\n",
       "4. 'G'\n",
       "5. 'I'\n",
       "6. 'K'\n",
       "7. 'M'\n",
       "8. 'O'\n",
       "9. 'Q'\n",
       "10. 'S'\n",
       "11. 'U'\n",
       "12. 'W'\n",
       "13. 'Y'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] \"A\" \"C\" \"E\" \"G\" \"I\" \"K\" \"M\" \"O\" \"Q\" \"S\" \"U\" \"W\" \"Y\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'B'</li>\n",
       "\t<li>'C'</li>\n",
       "\t<li>'D'</li>\n",
       "\t<li>'F'</li>\n",
       "\t<li>'G'</li>\n",
       "\t<li>'H'</li>\n",
       "\t<li>'J'</li>\n",
       "\t<li>'K'</li>\n",
       "\t<li>'L'</li>\n",
       "\t<li>'M'</li>\n",
       "\t<li>'N'</li>\n",
       "\t<li>'P'</li>\n",
       "\t<li>'Q'</li>\n",
       "\t<li>'R'</li>\n",
       "\t<li>'S'</li>\n",
       "\t<li>'T'</li>\n",
       "\t<li>'V'</li>\n",
       "\t<li>'W'</li>\n",
       "\t<li>'X'</li>\n",
       "\t<li>'Y'</li>\n",
       "\t<li>'Z'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'B'\n",
       "\\item 'C'\n",
       "\\item 'D'\n",
       "\\item 'F'\n",
       "\\item 'G'\n",
       "\\item 'H'\n",
       "\\item 'J'\n",
       "\\item 'K'\n",
       "\\item 'L'\n",
       "\\item 'M'\n",
       "\\item 'N'\n",
       "\\item 'P'\n",
       "\\item 'Q'\n",
       "\\item 'R'\n",
       "\\item 'S'\n",
       "\\item 'T'\n",
       "\\item 'V'\n",
       "\\item 'W'\n",
       "\\item 'X'\n",
       "\\item 'Y'\n",
       "\\item 'Z'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'B'\n",
       "2. 'C'\n",
       "3. 'D'\n",
       "4. 'F'\n",
       "5. 'G'\n",
       "6. 'H'\n",
       "7. 'J'\n",
       "8. 'K'\n",
       "9. 'L'\n",
       "10. 'M'\n",
       "11. 'N'\n",
       "12. 'P'\n",
       "13. 'Q'\n",
       "14. 'R'\n",
       "15. 'S'\n",
       "16. 'T'\n",
       "17. 'V'\n",
       "18. 'W'\n",
       "19. 'X'\n",
       "20. 'Y'\n",
       "21. 'Z'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] \"B\" \"C\" \"D\" \"F\" \"G\" \"H\" \"J\" \"K\" \"L\" \"M\" \"N\" \"P\" \"Q\" \"R\" \"S\" \"T\" \"V\" \"W\" \"X\"\n",
       "[20] \"Y\" \"Z\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "LETTERS[(1:12)]\n",
    "LETTERS[seq(from = 1, to = 26, by = 2)]\n",
    "LETTERS[c(-1, -5, -9, -15, -21)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 邏輯運算符\n",
    "\n",
    "如我們上面選擇特定的向量一樣，我們使用 `>`，`<` 這一類比較運算符來反悔一個邏輯向量（布爾值），這裡我們來演示一下： <br>\n",
    "```R\n",
    "> x <= 2 # less than or equal to \n",
    "[1] FALSE FALSE TRUE FALSE TRUE \n",
    "\n",
    "> x == 2 # equal to\n",
    "[1] FALSE FALSE TRUE FALSE FALSE \n",
    "\n",
    "> x != 2 # not equal to\n",
    "[1]  TRUE  TRUE FALSE  TRUE  TRUE\n",
    "```\n",
    "注意到了我們使用的雙等號`==`，這個和單等號不同，這是表達了等號兩邊做比較（和其他語言類似）意為“是否等於”；`!=` 則是“是否不等於”。\n",
    "```R\n",
    "> (x > 0) & (x < 10) # 'and' \n",
    "[1] TRUE TRUE TRUE FALSE FALSE\n",
    "```\n",
    "`&` 預算符相當於兩邊做交集 \"and\"，和 C 語言中的 `&&` 一樣；同樣的，如果是一條豎線`|`則代表了做併集；`!` 則這是否定運算。\n",
    "```R\n",
    "> (x == 5) | (x > 10)\n",
    "[1]  TRUE FALSE FALSE  TRUE FALSE\n",
    "\n",
    "> !(x > 5)\n",
    "[1]  TRUE FALSE  TRUE FALSE  TRUE\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "FALSE"
      ],
      "text/latex": [
       "FALSE"
      ],
      "text/markdown": [
       "FALSE"
      ],
      "text/plain": [
       "[1] FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "FALSE"
      ],
      "text/latex": [
       "FALSE"
      ],
      "text/markdown": [
       "FALSE"
      ],
      "text/plain": [
       "[1] FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "FALSE"
      ],
      "text/latex": [
       "FALSE"
      ],
      "text/markdown": [
       "FALSE"
      ],
      "text/plain": [
       "[1] FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "TRUE"
      ],
      "text/latex": [
       "TRUE"
      ],
      "text/markdown": [
       "TRUE"
      ],
      "text/plain": [
       "[1] TRUE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "TRUE"
      ],
      "text/latex": [
       "TRUE"
      ],
      "text/markdown": [
       "TRUE"
      ],
      "text/plain": [
       "[1] TRUE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "FALSE"
      ],
      "text/latex": [
       "FALSE"
      ],
      "text/markdown": [
       "FALSE"
      ],
      "text/plain": [
       "[1] FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x <= 2 # less than or equal to \n",
    "x == 2 # equal to\n",
    "(x > 0) & (x < 10) # 'and' \n",
    "(x == 5) | (x > 10)\n",
    "(x == 5) | (x > 10)\n",
    "!(x > 5) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 2.6. \n",
    "The function `rnorm()` generates normal random variables. For instance, `rnorm(10)` gives a vector of `10` i.i.d. standard normals. Gen- erate 20 standard normals, and store them as x. Then obtain subvectors of<br>\n",
    "(i) the entries in x which are less than 1; <br>\n",
    "(ii) the entries between $\\frac{1}{2}$ and 1;<br>\n",
    "(iii) the entries whose absolute value is larger than 1.5.<br>\n",
    "**Answer:**\n",
    "```R\n",
    "> x = rnorm(20)\n",
    "\n",
    "# question one\n",
    "> x_1 = c(x < 1)\n",
    "> x_1\n",
    " [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE\n",
    "[13]  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE\n",
    "\n",
    "#question two \n",
    "> x_2 = c(1/2 < x)\n",
    "> x_2\n",
    " [1]  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE\n",
    "[13] FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE\n",
    "\n",
    "#question three \n",
    "> x_3 = c(abs(x) > 1.5)\n",
    "> x_3\n",
    " [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE\n",
    "[13] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE\n",
    "``` "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符型向量\n",
    "\n",
    "向量不一定要包含数字，我们也可以创造一个字符型向量，其中每个条目都是一个文本自负串。在 R 语言，我们使用双引号去确定字符串：\n",
    "```R\n",
    "> x <-c(\"Hello\", \"how do you do\", \"lovely to meet your\", 42)\n",
    "> x \n",
    "\n",
    "[1] \"Hello\", \"how do you do\", \"lovely to meet your\"\n",
    "[4] 42\n",
    "```\n",
    "注意，不能将字符串和数字混合，如果要将数字和字符串混合的话，需要先把数字转化成字符串，然后才放进同一个向量；否则，字符串会变成数字对应物（ASCII）\n",
    "```R \n",
    "> x[2:3]\n",
    "[1] \"how do you do\" \"lovely to meet your\"\n",
    "\n",
    "> x[-4]\n",
    "[1] \"Hello\"    \"how do you do\"  \"lovely to meet your\"\n",
    "\n",
    "> c(x[1:2],\"goodbye\")\n",
    "[1] \"Hello\"  \"how do you do\" \"goodbye\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'Hello'</li>\n",
       "\t<li>'how do you do'</li>\n",
       "\t<li>'lovely to meet your'</li>\n",
       "\t<li>'42'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'Hello'\n",
       "\\item 'how do you do'\n",
       "\\item 'lovely to meet your'\n",
       "\\item '42'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'Hello'\n",
       "2. 'how do you do'\n",
       "3. 'lovely to meet your'\n",
       "4. '42'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"Hello\"               \"how do you do\"       \"lovely to meet your\"\n",
       "[4] \"42\"                 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'how do you do'</li>\n",
       "\t<li>'lovely to meet your'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'how do you do'\n",
       "\\item 'lovely to meet your'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'how do you do'\n",
       "2. 'lovely to meet your'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"how do you do\"       \"lovely to meet your\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'Hello'</li>\n",
       "\t<li>'how do you do'</li>\n",
       "\t<li>'lovely to meet your'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'Hello'\n",
       "\\item 'how do you do'\n",
       "\\item 'lovely to meet your'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'Hello'\n",
       "2. 'how do you do'\n",
       "3. 'lovely to meet your'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"Hello\"               \"how do you do\"       \"lovely to meet your\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'Hello'</li>\n",
       "\t<li>'how do you do'</li>\n",
       "\t<li>'goodbye'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'Hello'\n",
       "\\item 'how do you do'\n",
       "\\item 'goodbye'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'Hello'\n",
       "2. 'how do you do'\n",
       "3. 'goodbye'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"Hello\"         \"how do you do\" \"goodbye\"      "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x <-c(\"Hello\", \"how do you do\", \"lovely to meet your\", 42)\n",
    "x\n",
    "x[2:3]\n",
    "x[-4]\n",
    "c(x[1:2],\"goodbye\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵\n",
    "\n",
    "在統計學中我們經常會用到矩陣，因此，我們需要學習如何在 R 語言中使用矩陣。要創建矩陣就要了解矩陣函數：`matrix()`，首先可以去了解一下這個函數：`?matrix()`：\n",
    "```R\n",
    "> matrix(1:12, nrow = 3, ncol = 4)\n",
    "     [,1] [,2] [,3] [,4]\n",
    "[1,]    1    4    7   10\n",
    "[2,]    2    5    8   11\n",
    "[3,]    3    6    9   12\n",
    "```\n",
    "這個被稱為“column-major order”，也就是列優先，這時我們也可以只給出一個維度：\n",
    "```R \n",
    "> matrix(1:12, nrow = 3)\n",
    "```\n",
    "除非，我們想要回收矢量（這句話我沒看懂）：\n",
    "```R\n",
    ">matrix(1:3, nrow = 3, ncol = 4)\n",
    "     [,1] [,2] [,3] [,4]\n",
    "[1,]    1    1    1    1\n",
    "[2,]    2    2    2    2\n",
    "[3,]    3    3    3    3\n",
    "```\n",
    "我們還可以通過命令讓它按列排序或者按行排序：\n",
    "```R\n",
    "> matrix(1:12, nrow=3, byrow=TRUE) # order in row \n",
    "> matrix(1:12, nrow=3, byrow=FALSE)# order in column\n",
    "``` \n",
    "R 語言中為了方便計算還有很多不同的特定的函數，例如：`diag()`, `%o%`:\n",
    "```R\n",
    "\n",
    "> diag(3)\n",
    "     [,1] [,2] [,3]\n",
    "[1,]    1    0    0\n",
    "[2,]    0    1    0\n",
    "[3,]    0    0    1\n",
    "\n",
    "> diag(1:3)\n",
    "     [,1] [,2] [,3]\n",
    "[1,]    1    0    0\n",
    "[2,]    0    2    0\n",
    "[3,]    0    0    3\n",
    "\n",
    "> 1:5 %o% 1:5 # %o% stand for the Outer product of Arrays\n",
    "     [,1] [,2] [,3] [,4] [,5]\n",
    "[1,]    1    2    3    4    5\n",
    "[2,]    2    4    6    8   10\n",
    "[3,]    3    6    9   12   15\n",
    "[4,]    4    8   12   16   20\n",
    "[5,]    5   10   15   20   25\n",
    "\n",
    "> 1:5 %*% 1:5 # %*% stand for the Matrix Multiplication \n",
    "     [,1]\n",
    "[1,]   55\n",
    "```\n",
    "最後一個運算符執行外積，因此他創建了一個 $(i，j)\\ \\  x_iy_j$ 的矩陣。 概括一下，`outer()`就是為兩個參數以上的任意函數$f$，創建一個$f(x_i，y_j)$的矩阵。 （稍后将详细介绍功能。）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## 數據結構\n",
    "\n",
    "了解一門語言，當然要從它的數據結構開始，否則無法操縱程序中的對象。\n",
    "<font color=\"gray\"> 對象很重要 </font> \n",
    "\n",
    "這裡出現了一個 `a == y[4]`is false $\\ \\Rightarrow 0.6 \\neq 0.6 $；簡單的說，這就是浮點數在計算機中表達有限制，不能以任意精度去存儲，所以尤其是微小的數字或者巨大的數字在運算式會有意外發生。下面在舉一個例子：\n",
    "判斷數值的相對大小從來不用大於小於或者等於號，也從來不目測，因為在福點數運算中，大小比較從來都不靠譜:http://cos.name/cn/topic/106794 <br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.6"
      ],
      "text/latex": [
       "0.6"
      ],
      "text/markdown": [
       "0.6"
      ],
      "text/plain": [
       "[1] 0.6"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.4"
      ],
      "text/latex": [
       "0.4"
      ],
      "text/markdown": [
       "0.4"
      ],
      "text/plain": [
       "[1] 0.4"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.6"
      ],
      "text/latex": [
       "0.6"
      ],
      "text/markdown": [
       "0.6"
      ],
      "text/plain": [
       "[1] 0.6"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "FALSE"
      ],
      "text/latex": [
       "FALSE"
      ],
      "text/markdown": [
       "FALSE"
      ],
      "text/plain": [
       "[1] FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = seq(0, 1, by = 0.2)\n",
    "y = seq(0, 1, by = 0.2)\n",
    "y[4]\n",
    "x[3]\n",
    "a = 1- x[3]\n",
    "a\n",
    "a == y[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>0</li>\n",
       "\t<li>0.1</li>\n",
       "\t<li>0.2</li>\n",
       "\t<li>0.3</li>\n",
       "\t<li>0.4</li>\n",
       "\t<li>0.5</li>\n",
       "\t<li>0.6</li>\n",
       "\t<li>0.7</li>\n",
       "\t<li>0.8</li>\n",
       "\t<li>0.9</li>\n",
       "\t<li>1</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 0\n",
       "\\item 0.1\n",
       "\\item 0.2\n",
       "\\item 0.3\n",
       "\\item 0.4\n",
       "\\item 0.5\n",
       "\\item 0.6\n",
       "\\item 0.7\n",
       "\\item 0.8\n",
       "\\item 0.9\n",
       "\\item 1\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 0\n",
       "2. 0.1\n",
       "3. 0.2\n",
       "4. 0.3\n",
       "5. 0.4\n",
       "6. 0.5\n",
       "7. 0.6\n",
       "8. 0.7\n",
       "9. 0.8\n",
       "10. 0.9\n",
       "11. 1\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>TRUE</li>\n",
       "\t<li>TRUE</li>\n",
       "\t<li>TRUE</li>\n",
       "\t<li>FALSE</li>\n",
       "\t<li>TRUE</li>\n",
       "\t<li>TRUE</li>\n",
       "\t<li>FALSE</li>\n",
       "\t<li>FALSE</li>\n",
       "\t<li>TRUE</li>\n",
       "\t<li>TRUE</li>\n",
       "\t<li>TRUE</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item TRUE\n",
       "\\item TRUE\n",
       "\\item TRUE\n",
       "\\item FALSE\n",
       "\\item TRUE\n",
       "\\item TRUE\n",
       "\\item FALSE\n",
       "\\item FALSE\n",
       "\\item TRUE\n",
       "\\item TRUE\n",
       "\\item TRUE\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. TRUE\n",
       "2. TRUE\n",
       "3. TRUE\n",
       "4. FALSE\n",
       "5. TRUE\n",
       "6. TRUE\n",
       "7. FALSE\n",
       "8. FALSE\n",
       "9. TRUE\n",
       "10. TRUE\n",
       "11. TRUE\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "FALSE"
      ],
      "text/latex": [
       "FALSE"
      ],
      "text/markdown": [
       "FALSE"
      ],
      "text/plain": [
       "[1] FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "FALSE"
      ],
      "text/latex": [
       "FALSE"
      ],
      "text/markdown": [
       "FALSE"
      ],
      "text/plain": [
       "[1] FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "seq(0, 1, 0.1)\n",
    "seq(0, 1, 0.1) == c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)\n",
    "3 - 0.7 + 0.4 == 0\n",
    "sqrt(2)^2 == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
